package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.valid.AddGroup;
import com.sky.vo.DishVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * TODO 找时间补完swagger
 */
@RestController("adminDishController")
@RequestMapping("/admin/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 添加菜品
     * @param dto
     * @return
     */
    @PostMapping
    public Result save(@RequestBody @Validated(AddGroup.class) DishDTO dto){
        dishService.save(dto);

        return Result.success();
    }

    /**
     * 菜品分页查询
     * @param dto
     * @return
     */
    @GetMapping("/page")
    public Result page(@Validated DishPageQueryDTO dto){
        PageResult pageResult = dishService.page(dto);
        return Result.success(pageResult);
    }

    /**
     * 菜品删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result dels(String ids){
        String[] split = ids.split(",");
        dishService.dels(split);

        return Result.success();
    }

    /**
     * 回显修改菜品
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result info(@PathVariable("id") String id){
        DishVO vo = dishService.info(id);
        return Result.success(vo);
    }

    /**
     * 套餐添加菜品路径“/dish/list?categoryId=16”和回显修改菜品路径“/dish/{id}”杂糅，多编写一个根据分类查询所有菜品的接口
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    public Result list(String categoryId, String name){
        Long id = null;
        if(null != categoryId)
            id = Long.parseLong(categoryId);
        List<DishVO> vos = dishService.listByCategoryIdOrName(id, name);
        return Result.success(vos);
    }

    /**
     * 修改菜品信息
     * @param dto
     * @return
     */
    @PutMapping
    public Result update(@RequestBody DishDTO dto){
        dishService.update(dto);

        return Result.success();
    }

    /**
     * 启用禁用菜品
     * @param id
     * @param status
     * @return
     */
    @PostMapping("/status/{status}")
    public Result status(String id, @PathVariable("status") String status){
        dishService.updateStatus(Long.parseLong(id), Integer.parseInt(status));

        return Result.success();
    }
}
